package main

import (
	"fmt"
	"sync"
	"time"
)

func main() {
	wg := sync.WaitGroup{}
	wg.Add(1)
	go func() {
		defer wg.Done()
		start := time.Now()
		defer fmt.Printf("logging job cost1=%v\n", time.Since(start)) //defer会对表达式提前求值，此处恒为0

		defer func() {
			fmt.Printf("logging job cost2=%v\n", time.Since(start)) // defer+闭包解决此问题
		}()

		fmt.Printf("logging job start=%v\n", start)
		time.Sleep(time.Second)
	}()
	wg.Wait()
	time.Sleep(time.Second)
}
